var tmpDom = [];
var htmlStartDom = null;
var objDrag = {};

function RenderDragForReservation()
{
	var blankDOM = $(".cell[date]").not($(".cell[date] div[order]").parent()).not($(".deactive").parent());
	var date = null;
	blankDOM.drag(DragObject);
	$(".cell[date]").drag("end",DropObject);
}

function DragObject( ev, dd ){
	dateFromAttr = getDateAttr($(this));
	dateToAttr = getDateAttr($(ev.target),ev);
	if(dateToAttr == null || dateToAttr == 'NaN-NaN-NaN')
		dateToAttr = date;
	else
		date = dateToAttr;
	var tmpDate = $.datepicker.parseDate("yy-mm-dd",$("tr#date").find("td:last").attr("date"));
	if(new Date(dateToAttr) > tmpDate)
		dateToAttr = tmpDate.toSystemFormat();

	objDrag.roomName = $(this).parents("[roomname]").attr("roomname");
	objDrag.roomType = $(this).parents("[roomtype]").attr("roomtype");
	var dateClick = dateFromAttr.split("-");
	var dateClickMsec = new Date(dateClick[0],dateClick[1]-1,dateClick[2]).getTime();
	var dateNow = new Date().toSystemFormat().split("-");
	var datNowMsec = new Date(dateNow[0],dateNow[1]-1,dateNow[2]).getTime();
	if( dateClickMsec >= datNowMsec){
		createReservationSpan(dateFromAttr,dateToAttr,objDrag.roomName,objDrag.roomType);
	}
}

function DropObject()
{
	BookingDrag(objDrag.roomName,objDrag.roomType,objDrag.dateFrom,objDrag.dateTo,objDrag.spann);
	//ClearSpanSelection();
	objDrag = {};
}

function getDateAttr(objDom)
{
	if(objDom == null || objDom.size() == 0 || objDom.get(0).tagName == null || objDom.get(0).tagName == undefined)
		return null;
	while(objDom.get(0)!=null && objDom.get(0).tagName != "TD" && objDom.get(0).tagName != "TABLE")
		objDom = objDom.parent();
	
	return objDom.attr("date");
}

function getDateAttr(objDom, ev)
{
	if(objDom == null || objDom.size() == 0 || objDom.get(0).tagName == null || objDom.get(0).tagName == undefined)
		return null;
	while(objDom.get(0)!=null && objDom.get(0).tagName != "TD" && objDom.get(0).tagName != "TABLE")
		objDom = objDom.parent();
	
	if(objDom.attr("colspan") != null)
	{
		var span = objDom.attr("colspan");
		if(ev!= null)
		{
			var increase = Math.floor((span * ev.offsetX) / $(ev.target).width());
			if(increase > 0)
			{
				var d = new Date(objDom.attr("date"));
				return d.addDate(increase).toSystemFormat();
			}
		}
	}
	return objDom.attr("date");
}

function createReservationSpan(from,to,roomName,roomType)
{
	objDrag.dateFrom = $.datepicker.parseDate("yy-mm-dd",from);
	objDrag.dateTo = $.datepicker.parseDate("yy-mm-dd",to);
	var dateIndex = objDrag.dateFrom;
	
	while(dateIndex < objDrag.dateTo)
	{
		if($("[roomName='" + roomName + "'][roomType='" + roomType + "'] [date='" + dateIndex.addDate(1).toSystemFormat() + "']").find("div[order]").size() > 0)
			break;
		dateIndex = dateIndex.addDate(1);
	}
	if(dateIndex > objDrag.dateTo)
		dateIndex = objDrag.dateTo;
	date = dateIndex.toSystemFormat();
	objDrag.dateTo = dateIndex;
	
	createSelectionElement();
};

function createSelectionElement()
{
	var startDom = $("[roomName='" + objDrag.roomName + "'][roomType='" + objDrag.roomType + "'] [date='" + objDrag.dateFrom.toSystemFormat() + "']");
	
	if(startDom.find("div.SlectedSpan").size() == 0)
		htmlStartDom = startDom.clone().html();
	var span = 1;
	if(startDom.attr("colspan") != null)
		span = parseInt(startDom.attr("colspan"));
	
	var newSpan = daysBetween(objDrag.dateFrom,objDrag.dateTo) + 1;

	var txtDivSpan = "";
	if(newSpan > 1){
		txtDivSpan = SELECT_SPAN_DAYS.format(newSpan);
	}
	else
		txtDivSpan = SELECT_SPAN_1DAY;

	var divSpan = '<div class="SlectedSpan">' + txtDivSpan + '</div>';
	objDrag.spann = newSpan;

	if(span == newSpan)
	{
		// check div not exist create new one
		if(startDom.find("div[order]").size() == 0)
		{
			startDom.html(divSpan);
		}
	}
	else if(span < newSpan)
	{
		startDom.attr("colspan",newSpan);
		for(var i=span;i< newSpan;i++)
		{
			var dom = $("[roomName='" + objDrag.roomName + "'][roomType='" + objDrag.roomType + "'] td[date='" + objDrag.dateFrom.addDate(i).toSystemFormat() + "']");
			tmpDom.push(dom);
			dom.remove();
		}
	}
	else
	{
		if(newSpan > 1)
			startDom.attr("colspan",newSpan);
		else
			startDom.removeAttr("colspan");
		$("div.SlectedSpan").html(txtDivSpan);
		for(var i = span;i>newSpan;i--)
		{
			var dom = tmpDom.pop();
			startDom.after(dom);

			if(dom!=null){
				dom.drag(DragObject);
				dom.drag("end",DropObject);
			}
		}
	}
};

function ClearSpanSelection()
{
	this.span = 1;
	if($(".SlectedSpan").size() == 0)
		return;
	
	var startDom = $(".SlectedSpan").parent();
	var span = parseInt(startDom.attr("colspan"));
	startDom.html(htmlStartDom);
	startDom.removeAttr("colspan");
	for(var i = span;i>1;i--)
	{
		var dom = tmpDom.pop();
		startDom.after(dom);

		if(dom!=null){
			dom.drag(DragObject);
			dom.drag("end",DropObject);
		}
	}
}

function CalRoomTaxForDisplay(cost,span)
{
	if(SharedData.General.incloudetax){
		var rs = {};
		var tax = 0;
		for(var i=0;i<SharedData.RoomTax.length;i++)
			if(SharedData.RoomTax[i].IsOnRoomTariff)
				tax += cost * (SharedData.RoomTax[i].CurrencyValue/100);
			else if(SharedData.RoomTax[i].IsPerNight)
				tax += SharedData.RoomTax[i].CurrencyValue * span;
			else if(SharedData.RoomTax[i].IsPerReservation)
				tax += SharedData.RoomTax[i].CurrencyValue;
		
		rs.displayCost = cost;
		rs.tax = tax;
		return rs;
	}
	else{
		var rs = {};
		var tax = 0;
		
		var k = 0;
		var perc = 0;
		for(var i =0;i<SharedData.RoomTax.length;i++){
			if(SharedData.RoomTax[i].IsOnRoomTariff)
				perc += SharedData.RoomTax[i].CurrencyValue;
			else if(SharedData.RoomTax[i].IsPerNight)
				k += SharedData.RoomTax[i].CurrencyValue * span;
			else if(SharedData.RoomTax[i].IsPerReservation)
				k += SharedData.RoomTax[i].CurrencyValue;
		}
		
		var x_realprice = ((cost-k)*100) / (100+perc);
		tax = ((perc * x_realprice)/100) + k;

		rs.displayCost = cost;
		rs.tax = tax;
		return rs;
	}
}

